package cn.hrlly.tao2shou.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.hrlly.tao2shou.R;
import cn.hrlly.tao2shou.presenter.FindPasswordPresenter;
import cn.hrlly.tao2shou.presenter.FindPasswordPresenterImpl;
import cn.hrlly.tao2shou.utils.GpsUtil;
import cn.hrlly.tao2shou.view.base.BaseActivity;
import cn.hrlly.tao2shou.view.view_interface.FindPasswordView;

public class FindPasswordActivity extends BaseActivity implements FindPasswordView{

    @Bind(R.id.etPhoneNumber)
    EditText etPhoneNumber;
    @Bind(R.id.etPassword)
    EditText etPassword;
    @Bind(R.id.etCode)
    EditText etCode;
    @Bind(R.id.btnConfirm)
    Button btnConfirm;
    @Bind(R.id.btnGetCode)
    Button btnGetCode;
    private FindPasswordPresenter findPasswordPresenter;
    private GpsUtil gpsUtil;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_find_password);
        //控件绑定
        ButterKnife.bind(this);
        //初始化findPasswordPresenter
        findPasswordPresenter=new FindPasswordPresenterImpl(this,this);
        //初始化定位工具
        gpsUtil=new GpsUtil(this);
        gpsUtil.startLocation();
    }

    //停止定位，关闭数据库
    @Override
    protected void onDestroy() {
        super.onDestroy();
        gpsUtil.stopLocation();
        findPasswordPresenter.closeDataBase();
    }

    //btnComfirm的点击事件
    @OnClick(R.id.btnConfirm)
    public void confirm(){
        if(!etCode.getText().toString().equals("")&&!etPassword.getText().toString().equals("")&&!etPhoneNumber.getText().toString().equals("")){
            if(etPhoneNumber.getText().toString().length()==11){
                findPasswordPresenter.findPassword();
            }else {
                showMessage(R.string.phone_number_is_error);
            }
        }else {
            showMessage(R.string.fill_incomplete);
        }
    }

    //btnGetCode的点击事件
    @OnClick(R.id.btnGetCode)
    public void getCode(){
        if(!etPhoneNumber.getText().toString().equals("")){
            if(etPhoneNumber.getText().toString().length()==11){
                findPasswordPresenter.getCode();
            }else {
                showMessage(R.string.phone_number_is_error);
            }
        }else {
            showMessage(R.string.fill_incomplete);
        }
    }

    //对activity的操作
    @Override
    public String getPhoneNumber() {
        return etPhoneNumber.getText().toString();
    }

    @Override
    public String getVerificationCode() {
        return etCode.getText().toString();
    }

    @Override
    public String getNewPassword() {
        return etPassword.getText().toString();
    }

    @Override
    public double getLongitude() {
        return gpsUtil.returnLongitude();
    }

    @Override
    public double getLantitude() {
        return gpsUtil.returnLantitude();
    }

    @Override
    public void startActivity() {
        Intent intent=new Intent(FindPasswordActivity.this,MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
        finish();
    }

    @Override
    public void showMessage(String errorMessage) {
        Toast.makeText(this,errorMessage,Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showMessage(int errorMessage) {
        Toast.makeText(this,errorMessage,Toast.LENGTH_SHORT).show();
    }
}
